{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import numpy as np\n",
    "import librosa\n",
    "import os\n",
    "from tqdm import tqdm"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "sample_rate = 22050\n",
    "fourier_window_size = 2048\n",
    "max_db = 100\n",
    "preemphasis = 0.97\n",
    "frame_shift = 0.0125\n",
    "frame_length = 0.05\n",
    "hop_length = int(sample_rate * frame_shift)\n",
    "win_length = int(sample_rate * frame_length)\n",
    "n_mels = 80\n",
    "ref_db = 20\n",
    "resampled = 5\n",
    "\n",
    "def get_spectrogram(audio_file):\n",
    "    y, sr = librosa.load(audio_file, sr = sample_rate)\n",
    "    y, _ = librosa.effects.trim(y)\n",
    "    y = np.append(y[0], y[1:] - preemphasis * y[:-1])\n",
    "    linear = librosa.stft(\n",
    "        y = y,\n",
    "        n_fft = fourier_window_size,\n",
    "        hop_length = hop_length,\n",
    "        win_length = win_length,\n",
    "    )\n",
    "    mag = np.abs(linear)\n",
    "    mel_basis = librosa.filters.mel(sample_rate, fourier_window_size, n_mels)\n",
    "    mel = np.dot(mel_basis, mag)\n",
    "    mel = 20 * np.log10(np.maximum(1e-5, mel))\n",
    "    mag = 20 * np.log10(np.maximum(1e-5, mag))\n",
    "    mel = np.clip((mel - ref_db + max_db) / max_db, 1e-8, 1)\n",
    "    mag = np.clip((mag - ref_db + max_db) / max_db, 1e-8, 1)\n",
    "    return mel.T.astype(np.float32), mag.T.astype(np.float32)\n",
    "\n",
    "def load_file(path):\n",
    "    fname = os.path.basename(path)\n",
    "    mel, mag = get_spectrogram(path)\n",
    "    t = mel.shape[0]\n",
    "    num_paddings = resampled - (t % resampled) if t % resampled != 0 else 0\n",
    "    mel = np.pad(mel, [[0, num_paddings], [0, 0]], mode = 'constant')\n",
    "    mag = np.pad(mag, [[0, num_paddings], [0, 0]], mode = 'constant')\n",
    "    return fname, mel.reshape((-1, n_mels * resampled)), mag"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 200/200 [00:25<00:00,  7.88it/s]\n"
     ]
    }
   ],
   "source": [
    "if not os.path.exists('mel_old'):\n",
    "    os.mkdir('mel_old')\n",
    "if not os.path.exists('mag_old'):\n",
    "    os.mkdir('mag_old')\n",
    "\n",
    "wav_files = [f for f in os.listdir('old') if f.endswith('.wav')]\n",
    "\n",
    "for fpath in tqdm(wav_files):\n",
    "    fname, mel, mag = load_file('old/' + fpath)\n",
    "    np.save('mel_old/{}'.format(fname.replace('wav', 'npy')), mel)\n",
    "    np.save('mag_old/{}'.format(fname.replace('wav', 'npy')), mag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "100%|██████████| 200/200 [00:25<00:00,  7.98it/s]\n"
     ]
    }
   ],
   "source": [
    "if not os.path.exists('mel_young'):\n",
    "    os.mkdir('mel_young')\n",
    "if not os.path.exists('mag_young'):\n",
    "    os.mkdir('mag_young')\n",
    "\n",
    "wav_files = [f for f in os.listdir('young') if f.endswith('.wav')]\n",
    "\n",
    "for fpath in tqdm(wav_files):\n",
    "    fname, mel, mag = load_file('young/' + fpath)\n",
    "    np.save('mel_young/{}'.format(fname.replace('wav', 'npy')), mel)\n",
    "    np.save('mag_young/{}'.format(fname.replace('wav', 'npy')), mag)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.8"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
